/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fri.businessLayer;

import fri.entiry.FrHistory;
import java.util.List;
import java.util.Vector;
import javax.ejb.Stateless;
import javax.persistence.Query;

/**
 *
 * @author NguyenDongUIT
 */
@Stateless
public class HistoryBean extends GenericDAO<FrHistory> implements HistoryBeanRemote {

    public HistoryBean() {
        super(FrHistory.class);
    }

    public HistoryBean(Class<FrHistory> entityClass) {
        super(entityClass);
    }

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

    @Override
    public List<FrHistory> getHistory(String userId) {
        try {
            
            
            Query q = em.createQuery("select  s from FrHistory s where s.userId.userId = '"+userId+"'");
            List<FrHistory> result = (List<FrHistory>) q.getResultList();
            
            return result;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
    }
   
    @Override
    public int addHistory(FrHistory history) {
        return this.save(history);
    }

    @Override
    public List<FrHistory> getHistoryByUserIdAndState(String userId, int state) {
        try {
            

            Query q = em.createQuery("select s from FrHistory s where s.status = '"+state+"' and s.userId.userId ='" + userId + "' order by s.dateCreated desc");
            List<FrHistory> result = (List<FrHistory>) q.getResultList();
            
            return result;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
    }

    @Override
    public int deleteHistory(String historyId) {
        
        FrHistory his = getHistoryById(historyId);
        

        return this.delete(his);

    }

    @Override
    public int deleteAllHistory(String userId, int state) {
        try
            
        {
        List<FrHistory> his = getHistoryByUserIdAndState(userId, state);
        for(FrHistory h:his)
        {
            this.delete(h);
        }
        return 1;
        }
        catch (Exception e) {
        
            return 0;
        }

        
    }

    @Override
    public int isHistoryFinished(String historyId) {
        FrHistory history = this.getHistoryById(historyId);
        return history.getStatus();
    }

    @Override
    public FrHistory getHistoryById(String historyId) {
        try {
            
            String sql = "select s from FrHistory s where s.historyId = '" + historyId + "'";
            Query q = em.createQuery(sql);
            
            FrHistory result = (FrHistory)q.getSingleResult();
            
            return result;
            
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
    }

    @Override
    public int changeStateHistory(String historyId, int state) {
        try {
            
            return  em.createQuery("UPDATE FrHistory SET status = '" + state + "' where historyId = '" + historyId + "'").executeUpdate();
            
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return 0;
        }
    }
    
    
    
    

}
